import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.JOptionPane;


public class Rocket {

	//OH CRAP i just realized that everything that shoots is doing things
	//in pixels not meters (we never convert back)
	
	final double maxTurnPerSec = 120* Math.PI/180;
	final double mass = .6; //Mass ratio to player. 
	final double forceMagnitude = 120; // kg*m/ s^2
	final double maxVel = 20;
	
	int framerate;
	int rotDir; //Rotation direction: 1 is CW, -1 is CCW.
	double angle;  //Current angle of rotation
	double maxTurnPerFrame; //How much to rotate each frame when tracking
	double dT;
	Point2D.Double pos /*px*/, vvec, fvec;
	
	Point playerLoc;


	boolean toDelete = false;

	//Warning-circle colors

	public Rocket( Point2D.Double pos, double angle /*same as parent turret angle*/, int framerate){

		this.pos = pos;
		this.framerate = framerate;
		this.dT = 1 / (double)framerate;
		this.maxTurnPerFrame = maxTurnPerSec/(double)framerate;
		this.angle = angle;
		this.rotDir = 1;
		fvec = new Point2D.Double(0,0);
		vvec = new Point2D.Double(0,0);
		
	}

	public void update(Player player){
		//TODO: calculate the next player position by basic d = vt integration (using a large dT for
		//purposeful inaccuracy, and use that player location instead of player.ctr

		double playerAngle = Math.atan2(player.ctr.x - pos.x, pos.y - player.ctr.y) ;
		playerAngle = (playerAngle<0)? Math.PI*2 + playerAngle : playerAngle; //Turn the [-pi,pi] range to [0, 2pi]
		playerAngle %= Math.PI*2;

		//Find the angle that the turret has to turn: positive means turn CCW, negative means turn CW.
		double deltaAngle = (playerAngle - angle);

		//Set which direction to turn
		if(deltaAngle > 0){
			if( deltaAngle > Math.PI){
				rotDir = -1;
			}else
				rotDir = 1;
		}else{
			if( deltaAngle < -Math.PI ){
				rotDir = 1;
			}else
				rotDir = -1;
		}
		
		
		
		//If turret is within 5degrees:
		//Set the turn speed to be slightly slower so we dont spaz back and forth over the target angle
		//Fire shots every time the frameCounter_shots reaches framesPerShot

		if(Math.abs( deltaAngle ) < maxTurnPerFrame){  

			//turn only as much as we want to, to face the playah: i.e., deltaAngle/framerate.
			angle= (angle + 3*(rotDir*deltaAngle/framerate) ) % (2*Math.PI);
			if(angle < 0)	angle = Math.PI*2 + angle; //keep angle bounded 'twix 0 and 2pi
		}else{ //turn as much as we can
			angle= (angle + rotDir*maxTurnPerFrame) % (2*Math.PI);
			if(angle < 0)	angle = Math.PI*2 + angle;
		}
		//Vector in this new force
		
		
		
		fvec.x = forceMagnitude * Math.sin ( angle );
		fvec.y = -forceMagnitude * Math.cos ( angle );
				
		vvec.x += fvec.x / mass * dT;
		vvec.y += fvec.y / mass * dT;		
		
		double magv;
		if((magv = vvec.distance(0, 0))> maxVel){
			vvec.x*= maxVel / magv;
			vvec.y*= maxVel / magv;
		}
		
		pos.x = GarbooMain.m2px(GarbooMain.px2m(pos.x) + vvec.x * dT);
		pos.y = GarbooMain.m2px(GarbooMain.px2m(pos.y) + vvec.y * dT);
		

	}


	public Point getPointLocation(){
		return new Point( (int)pos.x, (int)pos.y);		
	}


	public void reset(){
		//TODO: reset

	}


	public double getAngle(){
		return angle;
	}

	
}
